﻿using System;
using ExFlash.Filters;
using ExFlash.Records;

namespace ExFlash.Tags
{
    
    
    public class PlaceObject3Tag : PlaceObject2Tag
    {
        protected const int PlaceFlagHasImageIndex = 11;
        protected const int PlaceFlagHasClassNameIndex = 12;
        protected const int PlaceFlagHasCacheAsBitmapIndex = 13;
        protected const int PlaceFlagHasBlendModeIndex = 14;
        protected const int PlaceFlagHasFilterListIndex = 15;
        
        protected override int BitArraySize
        {
            get
            {
                return 16;
            }
        }

        public Boolean PlaceFlagHasImage
        {
            get
            {
                return mFlags[PlaceFlagHasImageIndex];
            }
            protected set
            {
                mFlags[PlaceFlagHasImageIndex] = value;
            }
        }

        public Boolean PlaceFlagHasClassName
        {
            get
            {
                return mFlags[PlaceFlagHasClassNameIndex];
            }
            protected set
            {
                mFlags[PlaceFlagHasClassNameIndex] = value;
            }
        }

        public Boolean PlaceFlagHasCacheAsBitmap
        {
            get
            {
                return mFlags[PlaceFlagHasCacheAsBitmapIndex];
            }
            protected set
            {
                mFlags[PlaceFlagHasCacheAsBitmapIndex] = value;
            }
        }

        public Boolean PlaceFlagHasBlendMode
        {
            get
            {
                return mFlags[PlaceFlagHasBlendModeIndex];
            }
            protected set
            {
                mFlags[PlaceFlagHasBlendModeIndex] = value;
            }
        }

        public Boolean PlaceFlagHasFilterList
        {
            get
            {
                return mFlags[PlaceFlagHasFilterListIndex];
            }
            protected set
            {
                mFlags[PlaceFlagHasFilterListIndex] = value;
            }
        }

        public StringRecord ClassName
        {
            get;
            protected set;
        }

        public FilterList SurfaceFilterList
        {
            get;
            protected set;
        }

        public BlendModes BlendMode
        {
            get;
            protected set;
        }

        public Boolean BitmapCache
        {
            get;
            protected set;
        }

        public PlaceObject3Tag(int len, int version, Int32 offset)
            : base(len, TagCodes.PlaceObject3, version, offset)
        {
        }
        
        public override void SwfDeserialize(BinaryBitReader reader)
        {
            long StartIndex = reader.Position;

            if (mSwfVersion >= 5)
            {
                PlaceFlagHasClipActions = reader.ReadBoolean();
            }
            else
            {
                reader.SkipBits(1);
            }

            PlaceFlagHasClipDepth = reader.ReadBoolean();
            PlaceFlagHasName = reader.ReadBoolean();
            PlaceFlagHasRatio = reader.ReadBoolean();
            PlaceFlagHasColorTransform = reader.ReadBoolean();
            PlaceFlagHasMatrix = reader.ReadBoolean();
            PlaceFlagHasCharacter = reader.ReadBoolean();
            PlaceFlagMove = reader.ReadBoolean();
            reader.SkipBits(3);
            PlaceFlagHasImage = reader.ReadBoolean();
            PlaceFlagHasClassName = reader.ReadBoolean();
            PlaceFlagHasCacheAsBitmap = reader.ReadBoolean();
            PlaceFlagHasBlendMode = reader.ReadBoolean();
            PlaceFlagHasFilterList = reader.ReadBoolean();
            reader.AlignBits();

            Depth = reader.ReadUInt16();

            if (PlaceFlagHasClassName || (PlaceFlagHasImage && PlaceFlagHasCharacter))
            {
                ClassName = new StringRecord(mSwfVersion);
                ClassName.SwfDeserialize(reader);
            }

            if (PlaceFlagHasCharacter)
            {
                CharacterId = reader.ReadUInt16();
            }

            if (PlaceFlagHasMatrix)
            {
                TransformMatrix = new MatrixRecord();
                TransformMatrix.SwfDeserialize(reader);
            }

            if (PlaceFlagHasColorTransform)
            {
                ColorTransform = new CXFormAlphaRecord();
                ColorTransform.SwfDeserialize(reader);
            }

            if (PlaceFlagHasRatio)
            {
                Ratio = reader.ReadUInt16();
            }

            if (PlaceFlagHasName)
            {
                Name = new StringRecord(mSwfVersion);
                Name.SwfDeserialize(reader);
            }

            if (PlaceFlagHasClipDepth)
            {
                ClipDepth = reader.ReadUInt16();
            }

            if (PlaceFlagHasFilterList)
            {
                SurfaceFilterList = new FilterList();
                SurfaceFilterList.SwfDeserialize(reader);
            }

            if (PlaceFlagHasBlendMode)
            {
                BlendMode = (BlendModes)reader.ReadByte();
            }

            if (PlaceFlagHasCacheAsBitmap)
            {
                BitmapCache = reader.ReadByte() > 0;
            }

            if (PlaceFlagHasClipActions)
            {
                ClipActions = new ClipActions(mSwfVersion);
                ClipActions.SwfDeserialize(reader);
            }
        }

        public override void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }
    }
}
